﻿using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarcodeDelay.FrameServer
{
    public class FrameAppSession : AppSession<FrameAppSession, BinaryRequestInfo>
    {
        private Object sendLock = new Object();
        public const int MAX_FRAME_LENGTH = 255;

        protected override void HandleException(Exception e)
        {

        }

        public void SyncSend(string message)
        {
            lock (sendLock)
            {
                this.Send(message);
            }
        }

        public void SyncSend(byte[] content)
        {
            int contentLen = content.Length;

            if (contentLen > MAX_FRAME_LENGTH)
            {
                //SimbaCameraScale.Common.Log.Logger.LogInfo("FrameAppSession", "Send content too long than frame max length");
                return;
            }

            byte bLen = (byte)contentLen;
            byte[] sendBuffer = new byte[contentLen + 1];
            sendBuffer[0] = bLen;
            System.Array.Copy(content, 0, sendBuffer, 1, sendBuffer.Length - 1);

            lock (sendLock)
            {
                this.Send(sendBuffer, 0, sendBuffer.Length);
            }
        }
    }
}
